package com.yuanzi.base.redis;

import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuanzi.base.util.LogUtils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

/**
 * redis操作
 * @author chengshx
 * @date 2017年5月5日
 */
@Service
public class BaseRedisService implements IRedisService {
	
	@Autowired
	private JedisPool jedisPool;
	
	/**
	 * 存储string类型
	 * @author chengshx
	 * @param key
	 * @param value
	 * @return
	 */
	@Override
	public void set(String key, String value) {
		if(key == null || value == null){
			LogUtils.error(this.getClass(), "key and value cannot be null, key = {}, value = {}", key, value);
			return;
		}
		this.setEx(key, value, null);
	}

	/**
	 * 存储string类型，并设置key的有效时间
	 * @author chengshx
	 * @param key
	 * @param value
	 * @param seconds 过期时间，单位秒，为null不过期
	 * @return
	 */
	@Override
	public void setEx(String key, String value, Integer seconds) {
		if(key == null || value == null){
			LogUtils.error(this.getClass(), "key and value cannot be null, key = {}, value = {}", key, value);
			return;
		}
		Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();
            if(seconds == null){
            		jedis.set(key, value);  
            } else {
            		jedis.setex(key, seconds, value);
            }
        } finally {
        	if(jedis != null){
        		jedis.close();
        	}
        }
	}
	
	/**
	 * 获取string类型的值
	 * @author chengshx
	 * @param key
	 * @return
	 */
	@Override
	public String get(String key) {
		if(key == null){
			LogUtils.error(this.getClass(), "key cannot be null", "");
			return null;
		}
		Jedis jedis = null;
		String value = null;
        try {  
            jedis = jedisPool.getResource();
            value = jedis.get(key);
        } finally {  
	        	if(jedis != null){
	        		jedis.close();
	        	} 
        }
        return value;
    }
	
	/**
	 * 批量获取String类型的值
	 * @author chengshx
	 * @param key
	 * @param fields
	 * @return
	 */
	@Override
	public List<String> mget(String... keys) {
		if(keys == null){
			LogUtils.error(this.getClass(), "keys cannot be null", "");
			return null;
		}
		Jedis jedis = null;
		List<String> list = null;
        try {  
            jedis = jedisPool.getResource();
            list = jedis.mget(keys);
        } finally {  
	        	if(jedis != null){
	        		jedis.close();
	        	} 
        }
        return list;
	}
	
	/**
	 * 删除key
	 * @author chengshx
	 * @param keys
	 */
	@Override
	public void del(String... keys) {
		if(keys == null){
			LogUtils.error(this.getClass(), "keys cannot be null", "");
			return;
		}
		Jedis jedis = null;  
        try {  
            jedis = jedisPool.getResource();
            jedis.del(keys);
        } finally {
	        	if(jedis != null){
	        		jedis.close();
	        	}
        }
	}
	
	/**
	 * 设置key的过期时间
	 * @author chengshx
	 * @param key
	 * @param seconds 过期时间，单位秒
	 * @return
	 */
	@Override
	public void expire(String key, Integer seconds) {
		if(key == null){
			LogUtils.error(this.getClass(), "key cannot be null", "");
			return;
		}
		Jedis jedis = null;
        try {  
            jedis = jedisPool.getResource();
            jedis.expire(key, seconds);
        } finally {  
	        	if(jedis != null){
	        		jedis.close();
	        	} 
        }
	}
	
	/**
	 * 存储hash类型的值
	 * @author chengshx
	 * @param key hash表的名称
	 * @param field
	 * @param value
	 * @return
	 */
	@Override
	public void hset(String key, String field, String value) {
		if(key == null || field == null || value == null){
			LogUtils.error(this.getClass(), "key and field and value cannot be null, key = {}, field = {}", key, field);
			return;
		}
		Jedis jedis = null;
        try {  
            jedis = jedisPool.getResource();
            jedis.hset(key, field, value);
        } finally {  
	        	if(jedis != null){
	        		jedis.close();
	        	} 
        }
	}

	/**
	 * 批量存储hash类型的值
	 * @author chengshx
	 * @param key hash表的名称
	 * @param hash
	 * @return
	 */
	@Override
	public void hmset(String key, Map<String, String> hash) {
		if(key == null || hash == null){
			LogUtils.error(this.getClass(), "key and hash cannot be null", "");
			return;
		}
		Jedis jedis = null;
        try {  
            jedis = jedisPool.getResource();
            jedis.hmset(key, hash);
        } finally {  
	        	if(jedis != null){
	        		jedis.close();
	        	} 
        }
	}
	
	/**
	 * 获取hash类型的值
	 * @author chengshx
	 * @param key
	 * @param filed
	 * @return
	 */
	@Override
	public String hget(String key, String field) {
		if(key == null || field == null){
			LogUtils.error(this.getClass(), "key and field cannot be null, key = {}, field = {}", key, field);
			return null;
		}
		Jedis jedis = null;
		String value = null;
        try {  
            jedis = jedisPool.getResource();
            value = jedis.hget(key, field);
        } finally {  
	        	if(jedis != null){
	        		jedis.close();
	        	} 
        }
        return value;
	}
	
	/**
	 * 批量获取hash类型的值
	 * @author chengshx
	 * @param key
	 * @param fields
	 * @return
	 */
	@Override
	public List<String> hmget(String key, String...fields) {
		if(key == null || fields == null){
			LogUtils.error(this.getClass(), "key and fields cannot be null, key = {}, fields = {}", key, fields);
			return null;
		}
		Jedis jedis = null;
		List<String> list = null;
        try {  
            jedis = jedisPool.getResource();
            list = jedis.hmget(key, fields);
        } finally {  
	        	if(jedis != null){
	        		jedis.close();
	        	} 
        }
        return list;
	}
	
	/**
	 * 删除hash类型的值
	 * @author chengshx
	 * @param key
	 * @param fields
	 */
	@Override
	public void hdel(String key, String... fields) {
		if(key == null || fields == null){
			LogUtils.error(this.getClass(), "key and fields cannot be null, key = {}, fields = {}", key, fields);
			return;
		}
		Jedis jedis = null;
        try {  
            jedis = jedisPool.getResource();
            jedis.hdel(key, fields);
        } finally {  
	        	if(jedis != null){
	        		jedis.close();
	        	} 
        }
	}
	
	/**
	 * 获取hash的所有field
	 * @author chengshx
	 * @date 2017年7月12日
	 *
	 * @param key
	 * @return
	 */
	@Override
	public Set<String> hkeys(String key) {
		if(key == null){
			LogUtils.error(this.getClass(), "key cannot be null", "");
			return null;
		}
		Jedis jedis = null;
		Set<String> set = null;
        try {  
            jedis = jedisPool.getResource();
            set = jedis.hkeys(key);
        } finally {  
	        	if(jedis != null){
	        		jedis.close();
	        	} 
        }
        return set;
	}
	
	/**
	 * 获取hash的所有field和value
	 * @author chengshx
	 * @date 2017年9月25日
	 *
	 * @param key
	 * @return
	 */
	public Map<String, String> hgetAll(String key) {
		if (key == null) {
			LogUtils.error(this.getClass(), "key cannot be null", "");
			return null;
		}
		Jedis jedis = null;
		Map<String, String> map = null;
		try {
			jedis = jedisPool.getResource();
			map = jedis.hgetAll(key);
		} finally {
			if (jedis != null) {
				jedis.close();
			}
		}
		return map;
	}

	/**
	 * 获取hash的所有value
	 * @author chengshx
	 * @date 2017年7月12日
	 *
	 * @param key
	 * @return
	 */
	@Override
	public List<String> hvals(String key) {
		if(key == null){
			LogUtils.error(this.getClass(), "key cannot be null", "");
			return null;
		}
		Jedis jedis = null;
		List<String> list = null;
        try {  
            jedis = jedisPool.getResource();
            list = jedis.hvals(key);
        } finally {  
	        	if(jedis != null){
	        		jedis.close();
	        	} 
        }
        return list;
	}
	
	/**
	 * 递增num，num<0时递减
	 * @author chengshx
	 * @date 2017年8月7日
	 *
	 * @param key
	 * @param num
	 */
	public void incrby(String key, Long num) {
		if(key == null){
			LogUtils.error(this.getClass(), "key cannot be null", "");
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.incrBy(key, num);
		} finally {
			if(jedis != null){
        			jedis.close();
        		}
		}
	}
	
	/**
	 * 添加member
	 * @author chengshx
	 * @date 2017年12月25日
	 *
	 * @param key
	 * @param members
	 */
	public void sadd(String key, String... members) {
		if(key == null){
			LogUtils.error(this.getClass(), "key cannot be null", "");
		}
		Jedis jedis = null;
		try {
			jedis = jedisPool.getResource();
			jedis.sadd(key, members);
		} finally {
			if(jedis != null){
        			jedis.close();
			}
		}
	}
	
	/**
	 * 判断成员是否存在
	 * @author chengshx
	 * @date 2017年12月25日
	 *
	 * @param key
	 * @param member
	 * @return
	 */
	public Boolean sismember(String key, String member) {
		if(key == null){
			LogUtils.error(this.getClass(), "key cannot be null", "");
			return false;
		}
		Jedis jedis = null;
		Boolean result = false;
		try {
			jedis = jedisPool.getResource();
			result = jedis.sismember(key, member);
		} finally {
			if(jedis != null){
        			jedis.close();
			}
		}
		return result;
	}

	/**
	 * 设置key的确切失效时间
	 * @param key
	 * @param timestamp unix时间戳
	 * @return
	 */
	@Override
	public Long setExpireAt(String key, Long unixTime) {
		if(key == null){
			LogUtils.error(this.getClass(), "key cannot be null", "");
			return null;
		}
		
		Jedis jedis = null;
        try {  
            jedis = jedisPool.getResource();
            return jedis.expireAt(key, unixTime);
        } finally {  
	        	if(jedis != null){
	        		jedis.close();
	        	} 
        }
	}

	/**
	 * 获取member的集合
	 * @param key
	 * @return
	 * @return Set<String>
	 * @author choijung
	 */
	@Override
	public Set<String> smembers(String key) {
		if(key == null){
			LogUtils.error(this.getClass(), "key cannot be null", "");
			return null;
		}
		Jedis jedis = null;
		Set<String> result = null;
		try {
			jedis = jedisPool.getResource();
			result = jedis.smembers(key);
		} finally {
			if(jedis != null){
        			jedis.close();
			}
		}
		return result;
	}
	
}
